package com.hy.z.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.z.dto.UserBean;
import com.hy.z.entity.Role;
import com.hy.z.entity.User;
import com.hy.z.entity.UserRole;
import com.hy.z.mapper.RoleMapper;
import com.hy.z.mapper.UserMapper;
import com.hy.z.mapper.UserRoleMapper;
import com.hy.z.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.z.vo.UserInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Fhy
 * @since 2021-01-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 用户登陆
     *
     * @param userName
     * @param password
     * @return
     */
    @Override
    public UserBean getUserByUserName(String userName, String password) {
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            throw new RuntimeException("账号或密码不能为空");
        }
        User user = userMapper.selectOne(Wrappers.<User>lambdaQuery().select(User::getUserId, User::getUserName, User::getPassword)
                .eq(User::getUserName, userName));
        if (user == null) {
            throw new RuntimeException("该账号不存在!");
        }
        if (!password.equals(user.getPassword())) {
            throw new RuntimeException("密码错误!");
        }
        UserBean userBean = new UserBean();
        BeanUtils.copyProperties(user, userBean);
        return userBean;
    }
    /**
     * 查询用户信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage<UserInfoVo> getListByPage(long pageNum,long pageSize) {
        IPage<UserInfoVo> userIPage = userMapper.queryByPage(new Page<>(pageNum,pageSize));
        List<User> users = userMapper.selectList(null);

        //role中间对象
        Map<Integer, String> mapRole = new HashMap<>();
        List<Role> roles = roleMapper.selectList(null);
        for (Role role : roles) {
            mapRole.put(role.getId(),role.getRole());
        }

        List<Long> longList = users.stream().map(User::getUserId).collect(Collectors.toList());
        List<UserRole> userRoles = userRoleMapper.selectList(Wrappers.<UserRole>lambdaQuery().in(UserRole::getUserId, longList));

        //user中间map
        Map<Integer, Integer> mapUser = new HashMap<>();
        for (UserRole userRole : userRoles) {
            mapUser.put(userRole.getUserId(),userRole.getRoleId());
        }
        for (UserInfoVo userInfoVo : userIPage.getRecords()) {
            Integer roleId = mapUser.get(userInfoVo.getUserId().intValue());
            String roleName = mapRole.get(roleId);
            userInfoVo.setRole(roleName);
        }
        return userIPage;
    }
    //新增用户
    @Override
    public boolean addUser(User user) {
        return userMapper.insert(user) > 0;
    }
    /**
     * 删除用户
     */
    @Override
    public boolean deleteUser(Integer id) {
        int i = userMapper.deleteById(id);
        return i > 0;
    }
}
